// pages/02admin/02tableClass/tableClass.js
var app = getApp()
var appData = app.globalData
Page({
    data: {
        tableCateList: null
    },

    onLoad: function (options) {
        this.setData({
            tableCateList: app.globalData.tableCateArr
        })

        var that = this
        app.setNoticeTableCateList(function (updateData) {
            var arr = that.data.tableCateList
            var isHave = 0
            for (var i = 0; i < arr.length; i++){
                if (updateData.cateId == arr[i].cateId){
                    arr[i] = updateData
                    isHave++
                    break
                }
            }
            if(!isHave){
                arr.push(updateData)
            }
            that.setData({
                tableCateList:arr
            })
        })
        console.log('this.data.tableCateList',this.data.tableCateList)
        console.log('getApp().globalData.tableCateArr',getApp().globalData.tableCateArr)
    },

    editTableCate: function (e) {
        if (app.globalData.loginMode == 'boss') {
        let i = e.currentTarget.id
        var url_end=''
        if (i != '') {
            let oneCate = JSON.stringify(this.data.tableCateList[i])
            url_end = '?cate=' + oneCate
            console.log('url_end=='+url_end)
        }

        wx.navigateTo({
            url: '/pages/02admin/02setTableClass/setTableClass'+url_end//?cate=' + oneCate
        })
        }
        else {
            wx.showToast({
                title: '需要掌柜登录才能操作',
            })
        }
    },

    delTableCate: function (e) {
        if (app.globalData.loginMode == 'boss') {
        var i = e.currentTarget.id
        var list = this.data.tableCateList
        
        var param ={
            cateId: list[i].cateId,
            sql: 'delete from'
        }
        console.log('list[i]', list[i])
        var that = this
        app.getJsonData('api/admin/table_cate_save.php', param, function (res) {
            console.log('table_cate_sav:res',res)
            if(res.code == 0){
                list.splice(i, 1)
                that.setData({
                    tableCateList:list
                })
                console.log('that.data.tableCateList', that.data.tableCateList)
                res.data.notice = 'del'
                app.noticeUpdateTableCateList(res.data)//通知删除更新
            }else{
                wx.showModal({
                    title: '删除失败！',
                    content: res.message
                })
            }
        })
        }
        else {
            wx.showToast({
                title: '需要掌柜登录才能操作',
            })
        }
    },

})